using System;

namespace RpgServiceLib.Quest
{
	public class EasyQuestStatusHandlerStrategy:IQuestStatusHandlerStrategy
	{
		public EasyQuestStatusHandlerStrategy ()
		{
		}

		#region IQuestStatusHandlerStrategy implementation
		public void HandleStatusChange (IQuest quest, QuestStatus statusOld, QuestStatus statusNow)
		{
			if (statusNow == QuestStatus.Accomplished)
			{
				if ((quest.ListOfEmbeddedQuests.Count>0 && CheckIfAllSubquestAreAccomplished (quest))|| quest.ListOfEmbeddedQuests.Count == 0)
				{
					if (quest.LinkedFromQuest!=null)
					{
						RefreshStatus(quest.LinkedFromQuest);
					}
				}				
			}			
		}

		static bool CheckIfAllSubquestAreAccomplished (IQuest aquest)
		{
			foreach(var quest in aquest.ListOfEmbeddedQuests)
			{
				if (quest.StatusOfQuest!=QuestStatus.Accomplished)
				{
					return false;
				}
			}
			return true;
		}
		
		public void RefreshStatus (IQuest quest)
		{
			if (quest.ListOfEmbeddedQuests.Count>0 && CheckIfAllSubquestAreAccomplished (quest))
			{
				quest.SetQuestStatus(QuestStatus.Accomplished);
			}
		}
		
		#endregion
	}
}

